﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace Hopscotch.Presentation.Common.Converters
{
    /// <summary>
    /// Similar to the built-in BooleanToVisibilityConverter,
    /// except with the option to Invert or set the True/False Visibility values
    /// </summary>
    [ValueConversion(typeof(string), typeof(Visibility))]
    public class BooleanToVisibilityConverter : IValueConverter
    {
        /// <summary>
        /// The visibility value to convert to if source value is true
        /// </summary>
        public Visibility TrueVisibility { get; set; }

        /// <summary>
        /// The visibility value to convert to if source value is false
        /// </summary>
        public Visibility FalseVisibility { get; set; }

        /// <summary>
        /// Invert the input (input = !input)
        /// </summary>
        public bool InvertInput { get; set; }


        public BooleanToVisibilityConverter()
        {
            //Set the default True/False visibility
            TrueVisibility = Visibility.Visible;
            FalseVisibility = Visibility.Collapsed;
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                bool input = (bool)value;
                if (InvertInput)
                    input = !input;

                return input ? TrueVisibility : FalseVisibility;
            }

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (Equals(value, TrueVisibility))
            {
                return true ^ InvertInput;
            }
            if (Equals(value, FalseVisibility))
            {
                return false || InvertInput;
            }

            return value;
        }
    }
}
